package org.dromara.kitchen.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.kitchen.domain.product.Product;
import org.dromara.kitchen.domain.dto.TreeSelect;
import org.dromara.kitchen.mapper.product.ProductMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.dromara.kitchen.domain.product.ProductType;
import org.dromara.kitchen.mapper.product.ProductTypeMapper;
import org.dromara.kitchen.service.IProductTypeService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分类Service业务层处理
 *
 * @author Mr.Zhou
 * @date 2024-10-21
 */
@RequiredArgsConstructor
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductTypeMapper productTypeMapper;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;

//	@Value("${ruoyi.imgUrlOss}")
//	private String imgUrlOss;

    /**
     * 查询部门管理数据
     *
     * @param type 部门信息
     * @return 部门信息集合
     */
    @Override
    public List<ProductType> selectProductTypeList(ProductType type) {
        QueryWrapper<ProductType> qw = new QueryWrapper<>();
        qw.eq("state",0);
        if(type.getParentId()!=null) {
            qw.eq("parent_id",type.getParentId());
        }
        qw.orderByAsc("sort");
        List<ProductType> list = productTypeMapper.selectList(qw);
        list.forEach(productType -> {
            QueryWrapper<Product> qwProduct =new QueryWrapper<>();
            if(productType.getParentId()!=null) {
                qwProduct.eq("cate_child_id",productType.getId());
            }else{
                qwProduct.eq("cate_id",productType.getId());
            }
            qwProduct.eq("is_show",1);
            long productCount = productMapper.selectCount(qwProduct);
            productType.setProductCount(productCount);



            if(StrUtil.isNotEmpty(productType.getImgUrl())){
                if(StrUtil.isNotEmpty(productType.getImgUrl()) && productType.getImgUrl().indexOf("http") < 0){
                    productType.setImgPath(imgUrl + productType.getImgUrl());
                }else{
                    productType.setImgPath(productType.getImgUrl());
                }
            }else{
                productType.setImgPath("");
            }
        });
        return list;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildProductTypeTreeSelect(List<ProductType> depts) {
        List<ProductType> deptTrees = buildProductTypeTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    @Override
    public List<ProductType> buildProductTypeTree(List<ProductType> depts) {
        List<ProductType> returnList = new ArrayList<>();
        List<Integer> tempList = new ArrayList<>();
        for (ProductType dept : depts) {
            tempList.add(dept.getId());
        }
        for (ProductType dept : depts) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId())) {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty()) {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<ProductType> list, ProductType t) {
        // 得到子节点列表
        List<ProductType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ProductType tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }


    /**
     * 得到子节点列表
     */
    private List<ProductType> getChildList(List<ProductType> list, ProductType t) {
        List<ProductType> tlist = new ArrayList<>();
        for (ProductType n : list) {
            if (Validator.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ProductType> list, ProductType t) {
        return getChildList(list, t).size() > 0;
    }
}
